package cn.usian.jyzx.product.service.impl;

import cn.usian.jyzx.model.constant.Constant;
import cn.usian.jyzx.model.dto.h5.ProductSkuDto;
import cn.usian.jyzx.model.entity.product.Category;
import cn.usian.jyzx.model.entity.product.Product;
import cn.usian.jyzx.model.entity.product.ProductSku;
import cn.usian.jyzx.model.vo.h5.IndexVo;
import cn.usian.jyzx.model.vo.h5.ProductItemVo;
import cn.usian.jyzx.product.mapper.CategoryMapper;
import cn.usian.jyzx.product.mapper.ProductDetailsMapper;
import cn.usian.jyzx.product.mapper.ProductMapper;
import cn.usian.jyzx.product.mapper.ProductSkuMapper;
import cn.usian.jyzx.product.service.ProductService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductSkuMapper productSkuMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductDetailsMapper productDetailsMapper;

    @Override
    public IndexVo findall() {
//        查询一级分类
        List<Category> getall = getall();
        List<ProductSku> productSkuList = productSkuMapper.findSku();
        IndexVo indexVo = new IndexVo();
        indexVo.setCategoryList(getall);
        indexVo.setProductSkuList(productSkuList);
        return indexVo;
    }

    @Override
    public PageInfo<ProductSku> findpage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSku> productSkuList = productSkuMapper.findpage(productSkuDto);
        PageInfo<ProductSku> pageInfo = new PageInfo<>(productSkuList);
        return pageInfo;
    }

    @Override
    public ProductItemVo item(Long skuId) {
//        通过skuId查询ProductSku对象
        ProductSku productSku = productSkuMapper.item(skuId);
//        2.通过sku对象的productId查询product对象
        Product product = productMapper.getById(productSku.getProductId());
//        3.获取商品的规格
        String specValue = product.getSpecValue();
//       转换成json
        JSONArray specValueList = JSON.parseArray(specValue);
//        根据商品表(product)的id获取轮播图信息
        String ImageUrl =productDetailsMapper.getImageUrl(product.getId());
//      姜茶出来的轮播图信息分割转换成集合
        List<String> detailsImageUrlList = Arrays.asList(ImageUrl.split(","));
//        获取详情的图片
        String sliderUrls = product.getSliderUrls();
//        分割详情的图片转换成集合
        List<String> sliderUrlList = Arrays.asList(sliderUrls.split(","));
//        sku的规格，查询该商品下的所有sku。组合规格和对应的skuId
      List<ProductSku> productSkuList =   productSkuMapper.getall(product.getId());
        HashMap<String, Object> hashMap = new HashMap<>();
        for (ProductSku sku : productSkuList) {
            hashMap.put(sku.getSkuSpec(),sku.getId());
        }
//        最后一次添加到prooductItemVo对象中
        ProductItemVo productItemVo = new ProductItemVo();
        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        productItemVo.setDetailsImageUrlList(detailsImageUrlList);
        productItemVo.setSliderUrlList(sliderUrlList);
        productItemVo.setSpecValueList(specValueList);
        productItemVo.setSkuSpecValueMap(hashMap);
        return productItemVo;
    }


    public List<Category> getall(){
        String onefindall = (String) redisTemplate.opsForValue().get(Constant.ONe_findall);
        if (onefindall == null){
            List<Category> categoryList =  categoryMapper.findall();
            redisTemplate.opsForValue().set(Constant.ONe_findall, JSONObject.toJSONString(categoryList));
            return categoryList;
        }else {
            List<Category> categoryList = JSON.parseArray(onefindall, Category.class);
            return categoryList;
        }
    }
}
